package com.example.questionsystem.modules.qu.service.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChineseNumberConverter {
    private static final Map<Character, Character> CHINESE_NUMBER_MAP = new HashMap<>();
    static {
        CHINESE_NUMBER_MAP.put('一', '1');
        CHINESE_NUMBER_MAP.put('二', '2');
        CHINESE_NUMBER_MAP.put('三', '3');
        CHINESE_NUMBER_MAP.put('四', '4');
        CHINESE_NUMBER_MAP.put('五', '5');
        CHINESE_NUMBER_MAP.put('六', '6');
        CHINESE_NUMBER_MAP.put('七', '7');
        CHINESE_NUMBER_MAP.put('八', '8');
        CHINESE_NUMBER_MAP.put('九', '9');
    }

    private static final Pattern CHINESE_NUMBER_PATTERN = Pattern.compile("[一二三四五六七八九]");

    public static String extractAndConvertChineseNumber(String input) {
        Matcher matcher = CHINESE_NUMBER_PATTERN.matcher(input);
        if (matcher.find()) {
            char chineseNumber = matcher.group().charAt(0);
            return String.valueOf(CHINESE_NUMBER_MAP.getOrDefault(chineseNumber, chineseNumber));
        }
        return "";
    }

    public static void main(String[] args) {
        String str1 = "第一场";
        String str2 = "第二场";
        System.out.println(extractAndConvertChineseNumber(str1));
        System.out.println(extractAndConvertChineseNumber(str2));
    }
}